<?php
namespace control;

/*
*派遣结算类
*/

class laborSettleClass extends adIndexClass
{
    private const page = 'laborSettle';
    private const title = '派遣结算';

    /*列表-查询条件*/
    public function home()
    {
        //生成查询表单
        $html =
            text(array("name" => "orderNo", "title" => "单据编号")) .
            text(array("name" => "name", "title" => "派遣单位")) .
            text(array("name" => "createTime", 'class' => 'width200', 'laydate' => ['type' => 'date', 'range' => '~'], "title" => "单据日期-单据日期"));
        //返回
        return $this->homeFun($this->search($html));
    }

    /*客户列表-查询结果*/
    public function homeSearch($cla=self::page)
    {
        //like查询数组
        $like = array(
            'b.name' =>  $this->post('name'),
            'a.orderNo' =>  $this->post('orderNo'),
        );
        //全等查询数组
        $equal = array(
            'a.type'=>'派遣单位',
        );
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'targetDate');
        }
        //获取查询结果
        $where = search(array("like" => $like, "=" => $equal)) . $and;
        $page = $this->page(array("table" => "record as a join labor_company as b on a.typeId = b.id",'field'=>"b.orderNo,b.name,a.*", "where" => $where, "order" => "a.id desc"));
        $_SESSION[self::page]['sql'] = $page['fullSql'];
        //获取表头
        $tr[] = array('派遣单位','单据日期','单据编号','业务类型', '应付金额(' . static::$unit . ')', '实际付款金额(' . static::$unit . ')', '应付款余额(' . static::$unit . ')');
        //获取记录
        foreach ($page['array'] as $array) {
            if (in_array($array['classify'], ['劳动报酬付款'])) {
                $recMoney = $array['money'];
                $saleMoney = 0;
            } else {
                $saleMoney =  $array['money'];
                $recMoney =0;
            }
            $company = query('labor_company',"id = '$array[typeId]'");

            $tr[] = array($company['name'],$array['targetDate'],$array['orderNo'],$array['classify'],$saleMoney, $recMoney, $array['balance']!=0?$array['balance']:0);
        }
        //控件
        $button = "";
        if ($this->power($cla, "export")) {
            $button .= "  <a href='" . root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=exportData'><span class='spanButton'>导出</span></a>\n";
        }
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }


    /**
     * 导出
     */
    public function exportData()
    {
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $tr = [];
            $sql = $_SESSION[self::page]['sql'];
            $title =array('派遣单位','单据日期','单据编号','业务类型', '应付金额(' . static::$unit . ')', '实际付款金额(' . static::$unit . ')', '应付款余额(' . static::$unit . ')');
            $result = sqlFind($sql);
            if (is_array($result)) {
                foreach ($result as $array) {
                    if (in_array($array['classify'], ['劳动报酬付款'])) {
                        $recMoney = $array['money'];
                        $saleMoney = 0;
                    } else {
                        $saleMoney =  $array['money'];
                        $recMoney =0;
                    }
                    $company = query('labor_company',"id = '$array[typeId]'");

                    $tr[] = array($company['name'],$array['targetDate'],$array['orderNo'],$array['classify'],$saleMoney, $recMoney, $array['balance']!=0?$array['balance']:0);
                }
            }
            $text = $this->control['name'] . "导出了派遣结算表";
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $tr, "派遣结算表.xlsx");
        }
        header("Location:" . getenv("HTTP_REFERER"));
        exit(0);
    }
}

